package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.AuthenticateLoginContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DetailsWebViewActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/30
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */
public class AuthenticateLoginPresenter {
    private AuthenticateLoginContract authenticateLoginContract;
    private AppCompatActivity me;

    public AuthenticateLoginPresenter(AuthenticateLoginContract authenticateLoginContract, AppCompatActivity me) {
        this.authenticateLoginContract = authenticateLoginContract;
        this.me = me;
    }

    /**
     * 注册接口
     */
    public void getRegister(String userName, String verift, String psd) {
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.show("请填写用户名");
            return;
        }
        if (TextUtils.isEmpty(verift)) {
            ToastUtil.show("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(psd)) {
            ToastUtil.show("请输入密码");
            return;
        }
        authenticateLoginContract.showLoading();
        Model.getInstance().getUserApi().getRegisterBean("Member/register", userName, verift, psd).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                Map<String, String> map = Constants.codeGet(me, response);
                if (map == null) {
                    return;
                }
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                authenticateLoginContract.loginSuccess(data);
                authenticateLoginContract.hideLoading();
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                authenticateLoginContract.loginFailed(t.getMessage());
                authenticateLoginContract.hideLoading();
            }
        });
    }

    /**
     * 发送短信
     * login(登录)，register（注册）,retrieve（找回密码）,
     * mod_pass(修改密码)，mod_bind（修改绑定手机）
     * ，re_bind(绑定手机)
     */

    public void getSendVerity(String userName, String verift) {
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.show("请填写用户名");
            return;
        }
        if (TextUtils.isEmpty(verift)) {
            ToastUtil.show("数据错误");
            return;
        }
        authenticateLoginContract.showLoading();
        Model.getInstance().getUserApi().getSendVerify("Sms/sendVerify", userName, verift, "")
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        authenticateLoginContract.verifySuccess(map);
                        authenticateLoginContract.hideLoading();
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {
                        authenticateLoginContract.loginFailed(t.getMessage());
                        authenticateLoginContract.hideLoading();
                    }
                });
    }


    public void getSendVerity(String userName, String verift, String token) {
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.show("请填写用户名");
            return;
        }
        if (TextUtils.isEmpty(verift)) {
            ToastUtil.show("数据错误");
            return;
        }
        authenticateLoginContract.showLoading();
        Model.getInstance().getUserApi().getSendVerify("Sms/sendVerify", userName, verift, token).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                Map<String, String> map = Constants.codeGet(me, response);
                if (map == null) {
                    return;
                }
                authenticateLoginContract.verifySuccess(map);
                authenticateLoginContract.hideLoading();
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                authenticateLoginContract.loginFailed(t.getMessage());
                authenticateLoginContract.hideLoading();
            }
        });
    }

    /**
     * 注册协议
     */
    public void getAgreement() {
        Model.getInstance()
                .getUserApi()
                .getAgreement()
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        DetailsWebViewActivity.start(me, 4, data.get("registration_agreement"));
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 找回密码
     */
    public void getResetPassword(String account, String verify, String password, String type, final Activity activity) {
        if (TextUtils.isEmpty(account)) {
            ToastUtil.show("请填写用户名");
            return;
        }
        if (TextUtils.isEmpty(verify)) {
            ToastUtil.show("验证码不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show("修改的密码不能为空");
            return;
        }
        Model.getInstance().getUserApi().getResetPassword(account, verify, password, type)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ToastUtil.show(map.get("message"));
                        activity.finish();
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 换绑手机号
     */
    public void bindPhone1(String token, final String account, String verify) {
        if (TextUtils.isEmpty(account)) {
            ToastUtil.show("手机号不能为空！");
            return;
        }
        if (TextUtils.isEmpty(verify)) {
            ToastUtil.show("请输入验证码！");
            return;
        }
        authenticateLoginContract.showLoading();
        Model.getInstance().getUserApi().bindPhone1(token, account, verify)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Preferences.getInstance().commit(me, "name", "phone", account);
                        authenticateLoginContract.bindPhone1();
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 换绑手机号
     */
    public void bindPhone2(String token, final String account, String verify) {
        if (TextUtils.isEmpty(account)) {
            ToastUtil.show("手机号不能为空！");
            return;
        }
        if (TextUtils.isEmpty(verify)) {
            ToastUtil.show("请输入验证码！");
            return;
        }
        authenticateLoginContract.showLoading();
        Model.getInstance().getUserApi().bindPhone2(token, account, verify)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Preferences.getInstance().commit(me, "name", "phone", account);
                        authenticateLoginContract.bindPhone2();
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 三方登录
     */
    public void threeLoginBind(String bind_id, String account, String verify, String token) {
        if (TextUtils.isEmpty(account)) {
            ToastUtil.show("手机号不能为空！");
            return;
        }
        if (TextUtils.isEmpty(verify)) {
            ToastUtil.show("请输入验证码！");
            return;
        }
        authenticateLoginContract.showLoading();
        Model.getInstance().getUserApi().threeLoginBind(bind_id, account, verify, token)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Preferences.getInstance().commit(me, "name", "phone", account);
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        authenticateLoginContract.loginSuccess(data);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

}
